##### generate heatmap for all scenarios to provide overview of improvements over single best

library(BBmisc)
library(stringr)
library(plyr)
library(ggplot2)
source("results_utils.R")

# tmp = load2("~/cos/aslib-r/server_scripts/llama_results.RData")
# tmp = load2("~/work/coseal/aslib-r/server_scripts/llama_results.RData")
tmp = load2("../../aslib/server_scripts/llama_results.RData")

res = tmp$res
res$model2 = paste(res$algo, convertModelNames(res$model), sep = "/")
# hardcode order to be the same as in the paper
morder = c("classif/ksvm", "classif/randomForest", "classif/rpart", "regr/lm", "regr/randomForest", "regr/rpart", "cluster/XMeans")
res$model2 = factor(res$model2, levels=morder)
res$prob = convertScenarioNames(res$prob)
res$prob = factor(res$prob, levels = proborder)

measure = "par10"; sb = "singleBestByPar"
heat = ddply(res, .(prob), function(d) {
  sbscore = d[d$model == sb, measure]
  vbsscore = d[d$model == "vbs", measure]
  models = subset(d, d$algo != "baseline")
  models = subset(d, d$algo != "baseline")
  mscore = models[, measure]
  data.frame(
    algo = models$algo,
    model = models$model2,
    diff = sbscore - mscore,
    better = sbscore > mscore,
    factor = sbscore / mscore,
    gapclosed = 1 - (mscore - vbsscore) / (sbscore - vbsscore),
    best = (mscore == min(mscore)),
    fface = sapply(mscore, function(x) {
        if(x == min(mscore)) 4
        else if(x > sbscore) 3
        else 1
    })
  )
})

heat$prob = factor(heat$prob, levels = sort(unique(heat$prob), decreasing = TRUE))

# gm.mean = function(x, na.rm = TRUE){
#   exp(mean(log(x[x > 0]), na.rm = na.rm))
# }
# gm.mean.speedups = round(tapply(heat$gapclosed, heat$model, gm.mean), 2)

mean.speedups = setNames(sprintf("%.2f", tapply(heat$gapclosed, heat$model, mean)), morder)
best.scenario = setNames(sprintf("%.2f", tapply(heat$gapclosed, heat$prob, max)), 
  as.character(rev(unique(heat$prob))))
# options(xpd = T)
# p = ggplot(heat, aes(x = model, y = prob, fill = gapclosed, fontface = fface,
#   label = sprintf("%.2f", factor)))
# p = p + geom_tile(colour = "black")
# p = p + geom_text(aes(colour = (factor > mean(factor)))) + scale_colour_manual(values = c("black", "white"))
# p = p + scale_fill_gradient2("factor", low = "white", high = "black", breaks = log2(0:4), labels = 0:4)
# # p = p + geom_point(data = subset(heat, !better), shape = 4L, size = 3L)
# # p = p + geom_point(data = subset(heat, best), shape = 17L, size = 3L)
# p = p + labs(x = "", y = "")
# p = p + theme(
#   panel.background = element_blank(),
#   axis.text.x = element_text(angle = 45, hjust = 1)
# )
# p = p + guides(col = FALSE)
# p = p + annotate("text", x = names(gm.mean.speedups), y = 14.5, ymax = 15, label = gm.mean.speedups)

options(xpd = TRUE)
p = ggplot(heat, aes(x = model, y = prob, fill = gapclosed, fontface = fface,
  label = sprintf("%.2f", gapclosed)))
p = p + geom_tile(colour = "black", guide = "none")
p = p + geom_text(aes(colour = (gapclosed > mean(gapclosed)))) + scale_colour_manual(values = c("black", "white"))
p = p + scale_fill_gradient2("factor", low = "white", high = "black")
p = p + labs(x = "", y = "")
p = p + theme(
  panel.background = element_blank(),
  axis.text.x = element_text(angle = 45, hjust = 1)
)
p = p + guides(fill = "none", colour = "none")
p = p + annotate("text", x = names(mean.speedups), y = 14.5, ymax = 15, label = mean.speedups)
p = p + annotate("text", y = names(best.scenario), x = 8.25, xmax = 8.75, label = best.scenario)

ggsave("pics/res-heat-diff.pdf", width = 8, height = 5)
print(p)

warnings()
